package com.gzhu.knowledgeAdmin.common.helper;


import com.alibaba.fastjson.JSONObject;
import com.gzhu.knowledgeAdmin.entity.Permission;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 根据权限数据构建登录用户左侧菜单数据
 * </p>
 *
 * @author qy
 * @since 2019-11-11
 */
public class MemuHelper {

    /**
     * 构建菜单
     *
     * @param treeNodes
     * @return
     */
    public static List<JSONObject> bulid(List<Permission> treeNodes) {
        List<JSONObject> meuns = new ArrayList<>();
        List<Permission> oneMeunList;

        // 没有数据
        if (treeNodes == null || treeNodes.isEmpty()) {
            return meuns;
        }

        // 查询到菜单
        if (treeNodes.size() == 1) {
            Permission topNode = treeNodes.get(0);
            //左侧一级菜单
            oneMeunList = topNode.getChildren();
        } else {
            oneMeunList = treeNodes;
        }

        buildoneMenu(meuns, oneMeunList);

        return meuns;
    }

    private static void buildoneMenu(List<JSONObject> meuns, List<Permission> oneMeunList) {
        for (Permission one : oneMeunList) {
            JSONObject oneMeun = new JSONObject();
            oneMeun.put("path", one.getPath());
            oneMeun.put("component", one.getComponent());
            oneMeun.put("redirect", "noredirect"); // 以name作为菜单的唯一标识，是为了兼容前端框架
//                oneMeun.put("name", "name_"+one.getId());
            oneMeun.put("name", one.getName());
            oneMeun.put("hidden", false);

            JSONObject oneMeta = new JSONObject();
            oneMeta.put("title", one.getName());
            oneMeta.put("icon", one.getIcon());
            oneMeun.put("meta", oneMeta);

            List<JSONObject> children = new ArrayList<>();
            List<Permission> twoMeunList = one.getChildren();
            for (Permission two : twoMeunList) {
                JSONObject twoMeun = new JSONObject();
                twoMeun.put("path", two.getPath());
                twoMeun.put("component", two.getComponent());
                twoMeun.put("name", two.getName());
                twoMeun.put("hidden", false);

                JSONObject twoMeta = new JSONObject();
                twoMeta.put("title", two.getName());
                twoMeun.put("meta", twoMeta);

                children.add(twoMeun);

                List<Permission> threeMeunList = two.getChildren();
                for (Permission three : threeMeunList) {
                    if (StringUtils.isEmpty(three.getPath())) continue;

                    JSONObject threeMeun = new JSONObject();
                    threeMeun.put("path", three.getPath());
                    threeMeun.put("component", three.getComponent());
                    threeMeun.put("name", three.getName());
                    threeMeun.put("hidden", true);

                    JSONObject threeMeta = new JSONObject();
                    threeMeta.put("title", three.getName());
                    threeMeun.put("meta", threeMeta);

                    children.add(threeMeun);
                }
            }
            oneMeun.put("children", children);
            meuns.add(oneMeun);
        }
    }
}
